<?php
$map = array(
	array(0,0,0,0,0),
	array(0,1,2,1,0),
	array(0,0,1,1,0),
	array(0,3,3,3,0),
	array(0,0,0,0,0)
);

$count_x = count($map[0]);
$count_y = count($map);


// Create image instances
$dest = imagecreate($count_x * 32, $count_y *32);
$src = imagecreatefrompng('tileset.png');



for($y = 0; $y < $count_y; $y++) {
	for($x = 0; $x < $count_x; $x++) {
		imagecopy($dest, $src, $x * 32, $y * 32, $map[$y][$x] * 32, 0, 32, 32);
	}
}

// Output and free from memory
header('Content-Type: image/png');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);
?>